/*
 * @(#)MsgController.java 2020年3月2日 下午7:46:02
 * Copyright 2020 zmr, Inc. All rights reserved. 
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.wind.cqboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wind.cqboot.domain.GitCommit;
import com.wind.cqboot.domain.GitPush;
import com.wind.cqboot.domain.GitSender;
import com.wind.cqboot.utils.MsgUtil;

import cn.hutool.core.util.StrUtil;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotContainer;

/**
 * <p>File：MsgController.java</p>
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2020 2020年3月2日 下午7:46:02</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@RestController
@RequestMapping("send")
public class SendMsgController
{
    @Autowired
    private BotContainer botContainer;

    @RequestMapping("msg")
    public String msg(Long groupId, String msg)
    {
        Bot bot = botContainer.getBots().values().iterator().next();
        bot.sendGroupMsg(groupId, msg, false);
        return "ok";
    }

    @RequestMapping("hooks")
    public void hooks(Long groupId, String repName,
            @RequestBody GitPush gitPush)
    {
        Bot bot = botContainer.getBots().values().iterator().next();
        if (null != gitPush)
        {
            GitSender sender = gitPush.getSender();
            String msg = sender.getName() + "在 " + repName + " push了最新代码\r\n";
            List<GitCommit> commits = gitPush.getCommits();
            for (GitCommit gitCommit : commits)
            {
                msg += gitCommit.message + "\r\n";
            }
            bot.sendGroupMsg(groupId, msg, false);
        }
    }

    @RequestMapping("voice")
    public String voice(Long groupId, String path, String url)
    {
        Bot bot = botContainer.getBots().values().iterator().next();
        if (StrUtil.isNotBlank(url))
        {
            MsgUtil.sendGroupVoiceByUrl(bot, groupId, url);
        }
        else if (StrUtil.isNotBlank(path))
        {
            MsgUtil.sendGroupVoiceByPath(bot, groupId, path);
        }
        return "ok";
    }
}
